  SUBROUTINE TS_FILTER2
  implicit none
!------------------------------------------------------------------------
! --- for land mask
!------------------------------------------------------------------------
  integer, parameter :: ix0 =   40                         
  integer, parameter :: iy0 = 1620 
  integer, parameter ::  ix = 360                         
  integer, parameter ::  iy = 180 
  real,    parameter ::  pi = 180.0  
  real,    parameter :: tpi = 360.0
  real,    parameter :: hpi =  90.0

  character*120 :: &
! landmask = '/home/jjs/tstorms_wehner/source/trajectory/landsea.map'
  landmask = '/glade/u/home/zarzycki/tstorms/source/trajectory/landsea.map'
  integer, dimension(ix0,iy0) :: mask0
  integer, dimension(ix, iy ) :: mask
  real                        :: dlon, lon0, dlat, lat0

  integer, dimension(2) :: shape2 = (/ ix, iy /)

!------------------------------------------------------------------------
! --- for latitude bounds
!------------------------------------------------------------------------
  real :: nlat =  40.0
  real :: slat = -40.0

!------------------------------------------------------------------------

  real        :: xcyc, ycyc, wind, psl, vort
  integer     :: year, month, day,  hour, ii, jj, max
  logical     :: keeper
  character*5 :: dummy

!========================================================================

 103 FORMAT( 2f8.2, 4i6 )
 104 FORMAT( a5,    5i6 )
 105 FORMAT( 5f8.2, 4i6 ) 
 200 format( 40i2 )

!------------------------------------------------------------------------
! --- set up land mask
!------------------------------------------------------------------------

   OPEN ( 10, FILE = trim(landmask) ) 
   do jj = 1,iy0
       read ( 10, 200 ) (mask0(ii,jj), ii=1,ix0)
   enddo    
   CLOSE( 10 )

   mask = RESHAPE( mask0, shape2 )
   mask = CSHIFT ( mask,  ix/2, 1 )

   lon0 =  0.0
   dlon =  tpi / FLOAT( ix )
   lat0 = -hpi + 0.5 *   pi / FLOAT( iy )                       
   dlat =       -2.0 * lat0 / FLOAT( iy - 1 )  

!------------------------------------------------------------------------
! --- process data
!------------------------------------------------------------------------

      OPEN( 12, file='ori'       )
      OPEN( 14, file='ori_filt'  )
      OPEN( 22, file='traj'      )
      OPEN( 24, file='traj_filt' )

!--- read a record
  100 READ( 12,103,end=101 ) xcyc, ycyc, year, month, day, hour

!--- filter by latitude
    keeper = ( ycyc <= nlat ) .and. ( ycyc >= slat ) 

!--- filter out storms over land
         jj = ( ycyc - lat0 ) / dlat + 1.5
         ii = ( xcyc - lon0 ) / dlon + 1.5
    if ( ii == 0  ) ii = ix
    if ( ii >  ix ) ii = ii - ix
    keeper = keeper .and. ( mask(ii,jj) == 0 ) 

!--- write a record
    if( keeper ) then
!   ====================================================
    WRITE( 14,103 ) xcyc, ycyc, year, month, day, hour
    READ ( 22,104 ) dummy, max, year, month, day, hour
    WRITE( 24,104 ) dummy, max, year, month, day, hour
    do ii = 1, max
    READ ( 22, 105 ) xcyc, ycyc, wind, psl, vort, year, month, day, hour
    WRITE( 24, 105 ) xcyc, ycyc, wind, psl, vort, year, month, day, hour
    end do
!   ====================================================
    else
!   ====================================================
    READ ( 22,104 ) dummy, max, year, month, day, hour
    do ii = 1, max
    READ ( 22, 105 ) xcyc, ycyc, wind, psl, vort, year, month, day, hour
    end do
!   ====================================================
    endif

      go to 100
  101 continue
      CLOSE( 12 )
      CLOSE( 14 )
      CLOSE( 22 )
      CLOSE( 24 )

!========================================================================
  end SUBROUTINE TS_FILTER2